// pages/distribution-detail/detail.js
var model=require('../distribution/model/DistributionModel.js')
import SystemInfoUtil from '../../utils/SystemInfoUtil.js'
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    distributionObject:null,
    persionList:[],
    key:'',
    isIOS: false,
    _canShare:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    var _isIOS = SystemInfoUtil.platform == SystemInfoUtil.IOS
    this.setData({
      isIOS: _isIOS
    })
    var obj = JSON.parse(options.obj)
    this.setData({
      distributionObject:obj
    })
    model.getDistributionPersion(obj.id,1,function(list){
      for (var i=1;i < list.length;i++){
        if (list[i].agentCover == "https://web.fenbibox.com/chalkImages") {
          list[i].agentCover = '/images/userhead.png';
        }
      }
      that.setData({
        persionList:list
      })
    })
  },
  goCourseDetail:function(event){
    var id = this.data.distributionObject.courseNoExpand
    var title = this.data.distributionObject.courseText
    var _url = '../courseinfo/detail?courseId=' + id + '&title=' + title
    wx.navigateTo({
      url: _url,
    })
  },
  invokeDistribution:function(event){
    if (this.data.distributionObject==null){
      return
    }
    var that=this
    if (this.data.key.length<=0){ 
      model.generateDistributions(this.data.distributionObject.id, this.data.distributionObject.courseNoExpand, function (state, data) {
        if (state > 0) {
          var ak = that.getUrlParam("ak", data.url)
          that.setData({
            key: ak,
            _canShare: true
          })
        } else {
          wx.showToast({
            title: data,
            icon: 'none',
            duration: 3000
          })
        }
      })
    }
  },
   getUrlParam:function(name,url){
     if (url.indexOf('?') == 1) { return false; }
     url=url.split("?")[1]
     url = url.split('&');
     var name = name || '';
     var nameres;
     // 获取全部参数及其值
     for (var i = 0; i < url.length; i++) {
       var info = url[i].split('=');
       var obj = {};
       obj[info[0]] = decodeURI(info[1]);
       url[i] = obj;
     }
     // 如果传入一个参数名称，就匹配其值
     if (name) {
       for (var i = 0; i < url.length; i++) {
         for (const key in url[i]) {
           if (key == name) {
             nameres = url[i][key];
           }
         }
       }
     } else {
       nameres = url;
     }
     // 返回结果
     return nameres;
 },
 //查看更多代理人
 lookMorePersion:function(event){
   var _url = '../distribution-agent/agent?id='+this.data.distributionObject.id
    wx.navigateTo({
      url: _url,
    })
 },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var shareObject = {
      title: "我在粉笔乐学发现宝贝啦("+this.data.distributionObject.courseText+")",
      path: 'pages/courseinfo/detail?courseId=' + this.data.distributionObject.courseNoExpand + "&title=" + this.data.distributionObject.courseText+"&bk="+this.data.key,
      imageUrl: this.data.distributionObject.courseCover,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          icon: ''
        })
      }
    }
    console.log(shareObject)
    return shareObject;
  }
})